'''
2月10日的作业
传一个小说的链接，返回以下形式数据格式


book_info = Novel('http://www.x23us.us').get_book_info('http://www.x23us.us/39_39642/')#最终形态

book_info = {

    "book_name": [],
    "book_author": [],
    "book_intor": '',
    "book_chapter":{
        '第45章 不能冒险': 'http://www.x23us.us/39_39642/20371904.html',
         ... ...
    }
    "host":"",
}

'''

import re

import requests

import os.path

from pprint import pprint



class X23usNovel:

    host = 'http://www.x23us.us'

    book_info_regex = dict(

        book_name = '<meta property="og:novel:book_name" content="(.*?)" />',
        book_author = '<meta property="og:novel:author" content="(.*?)" />',
        book_intor = '<meta property="og:description" content="(.*?)"',
        book_chapter = "<dd><a href='(.*?)'>(.*?)</a></dd>",
    )

    chapter_info_regex = dict(

        chapter_name = '<h1>(.*?)</h1>',

        chapter_content = '<div id="content" name="content">((?is:.*?))</div>',

        next_chapter_url = '<a href="(http://www.x23us.us/\d+?_\d+?/\d+?.html)">下一章</a>',

        pre_chapter_url = '<a href="(http://www.x23us.us/\d+?_\d+?/\d+?.html)">上一章</a>',
    )

    def __init__(self):

        pass

    def get_book_info(self, url):#提取小说目录的信息

        Novel_info = requests.get(url)

        Novel_info.encoding = Novel_info.apparent_encoding

        html_str = Novel_info.text

        book_info = {}

        book_info['host'] = self.host

        for key,value in self.book_info_regex.items():

            book_info[key] = re.findall(value, html_str)

        book_info['book_intor'] = book_info['book_intor'][0]

        book_info['book_chapter'] = {i[1]: 'https://www.x23us.us' +i[0]for i in book_info['book_chapter']}

        return book_info

    def get_chapter_info(self, chapter_url):#提取小说章节
        '''
        chapter_url: 章节阅读页面
        return : dict

        {
            "chapter_name": '',
            "chapter_content": "",
            "chapter_url": "",
            "next_chapter_url": "",
            "pre_chapter_url": "",
            "host": ""
            "book_url": 
        }
        '''

        Novel_info = requests.get(chapter_url)

        Novel_info.encoding = Novel_info.apparent_encoding

        html_str = Novel_info.text

        info = {key: re.findall(value, html_str) for key, value in self.chapter_info_regex.items()}

        # print(info)
        if not info['pre_chapter_url']:
            info['pre_chapter_url'] = [chapter_url]
        if not info['next_chapter_url']:
            info['next_chapter_url'] = [chapter_url]

        for i in info:#字典循环返回键

            info[i] = info[i][0]
        
        info['chapter_url'] = chapter_url
        info['host'] = self.host
        info['book_url'] = os.path.dirname(chapter_url)


        return info

    def get_search_info(self, keywords):#搜索书信息

        '''
        keywords: 搜索的关键字
        return:dict
        {
            "keywords": '',
            "host": "",
            "search_info":[
                {'book_name': "", "book_url": "", "book_author": "", "book_status": ""},
                ... ...
            ]

        }
        '''

        search_url = 'http://www.x23us.us/modules/article/search.php'

        r = requests.get(search_url, params = {"q": keywords})

        r.encoding = r.apparent_encoding

        html = r.text

        #作业：把正则和数据写出来并清洗

        regex = """\
<tr id="nr">\s+?\
<td class="odd"><a href="(.*?)">(.*?)</a></td>\s+?\
<td class="even"><a href=".*?" target="_blank">.*?</a></td>\s+?\
<td class="odd">(.*?)</td>\s+?\
<td class="even">.*?</td>\s+?\
<td class="odd" align="center">.*?</td>\s+?\
<td class="even" align="center">(.*?)</td>\s+?\
</tr>"""
        info = re.findall(regex, html)

        search_info = [{'book_name': i[1], "book_url": i[0], "book_author": i[2], "book_status": i[3]} for i in info]

        info_dict = {

            "keywords": keywords,
            "host": self.host,
            "search_info": search_info

        }

        return info_dict

 

if __name__ == "__main__":
    
    book_info = X23usNovel().get_chapter_info('http://www.x23us.us/39_39642/16556658.html')

    print(book_info)


